package com.ede.roverhu.simplebackstage.Fragments;

/**
 * Created by Admin on 2017/6/20.
 */

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ede.roverhu.simplebackstage.Activities.ClassInfoActivity;
import com.ede.roverhu.simplebackstage.Activities.TimetableActivity;
import com.ede.roverhu.simplebackstage.Activities.UserInfoActivity;
import com.ede.roverhu.simplebackstage.Constant.HandlerConstant;
import com.ede.roverhu.simplebackstage.Domain.ClassListDomain;
import com.ede.roverhu.simplebackstage.Domain.QueryDomain;
import com.ede.roverhu.simplebackstage.Domain.SchoolBase;
import com.ede.roverhu.simplebackstage.Domain.UserInfoBase;
import com.ede.roverhu.simplebackstage.Domain.schoolClassBase;
import com.ede.roverhu.simplebackstage.Engine.htmlHander;
import com.ede.roverhu.simplebackstage.Http.HttpClient;
import com.ede.roverhu.simplebackstage.R;
import com.ede.roverhu.simplebackstage.UI.RefreshLayout;
import com.ede.roverhu.simplebackstage.UI.SelfDialog;
import com.ede.roverhu.simplebackstage.Utils.LogUtils;
import com.ede.roverhu.simplebackstage.Utils.ToastUtils;

import java.util.ArrayList;


public class PlaceholderFragment extends Fragment {
    private QueryDomain mqueryDomain;
    private peopleAdapter adapter;
    private ClassAdapter classAdapter;
    private TimetableAdapter timetableAdapter;
    private int page = 1;
    private ListView mlistview;
    private RefreshLayout swipeRefreshLayout;
    private View m_listViewFooter;
    private static final String ARG_SECTION_NUMBER = "section_number";


    private ArrayList<UserInfoBase> list;
    private ArrayList<SchoolBase> schoolList;
    private ArrayList<schoolClassBase> schoolClassList;
    private ArrayList<ClassListDomain> cllist;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String str = msg.obj.toString();
            switch (msg.what) {
                case HandlerConstant.QUERYMEMBER_SUCCESS4P1:
                    list = htmlHander.getPeopleList(getContext(), str);
                    schoolList = htmlHander.getSchoolList(getContext(), str);
                    adapter = new peopleAdapter(getContext(), list);
                    mlistview.setAdapter(adapter);
                    swipeRefreshLayout.setRefreshing(false);
                    swipeRefreshLayout.setLoading(false);
                    break;
                case HandlerConstant.QUERYMEMBER_FAIL:
                    ToastUtils.show(getActivity(), "查询失败");
                    break;

                case HandlerConstant.QUERYMEMBER_SUCCESS4PM:
                    list = htmlHander.getPeopleList(getContext(), str);
                    schoolList = htmlHander.getSchoolList(getContext(), str);
                    adapter.addlist(list);
                    adapter.notifyDataSetChanged();
                    swipeRefreshLayout.setRefreshing(false);
                    swipeRefreshLayout.setLoading(false);
                    break;

                case HandlerConstant.QUERYSCHOOLCLASS_SUCCESS:
                    schoolClassList = htmlHander.getSchoolClass(getContext(), str);
                    for (int i = 0; i < schoolClassList.size(); i++) {
                        LogUtils.e(schoolClassList.get(i).className);
                    }

                    classAdapter = new ClassAdapter(getContext(), schoolClassList);
                    mlistview.setAdapter(classAdapter);
                    swipeRefreshLayout.setRefreshing(false);
                    swipeRefreshLayout.setLoading(false);
                    break;
                case HandlerConstant.QUERYSCHOOLCLASS_SUCCESS4PM:
                    schoolClassList = htmlHander.getSchoolClass(getContext(), str);
//                    LogUtils.e(schoolClassList.get(0).className);
                    classAdapter.addlist(schoolClassList);
                    classAdapter.notifyDataSetChanged();
                    swipeRefreshLayout.setRefreshing(false);
                    swipeRefreshLayout.setLoading(false);
                    break;
                case HandlerConstant.QUERYSCHOOLCLASS_FAIL:
                    ToastUtils.show(getActivity(), "查询失败");
                    break;

                case HandlerConstant.QUERYTIMETABLE_SUCCESS:
                    cllist = htmlHander.getTimetable(getContext(), str);
                    timetableAdapter = new TimetableAdapter(getContext(), cllist);
                    mlistview.setAdapter(timetableAdapter);
                    swipeRefreshLayout.setRefreshing(false);
                    swipeRefreshLayout.setLoading(false);
                    break;
                case HandlerConstant.QUERYTIMETABLE_SUCCESS4PM:
                    cllist = htmlHander.getTimetable(getContext(), str);
                    timetableAdapter.addlist(cllist);
                    mlistview.setAdapter(timetableAdapter);
                    swipeRefreshLayout.setRefreshing(false);
                    swipeRefreshLayout.setLoading(false);
                    break;
                case HandlerConstant.QUERYTIMETABLE_FAIL:
                    ToastUtils.show(getActivity(), "查询失败");
                    break;
            }
        }
    };

    public PlaceholderFragment() {

    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //人员管理
        if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
            mqueryDomain = new QueryDomain();
            View rootView = inflater.inflate(R.layout.fragment_people, container, false);
            mlistview = (ListView) rootView.findViewById(R.id.listview);
            m_listViewFooter = LayoutInflater.from(getContext())
                    .inflate(R.layout.listview_footer, null, false);
            mlistview.addFooterView(m_listViewFooter);
            mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent(getContext(), UserInfoActivity.class);
                    intent.putExtra("iD", adapter.list.get(i).iD);
                    startActivity(intent);
                }
            });
            page = 1;
            HttpClient.queryMember(mqueryDomain, 1, handler);
            rootView.findViewById(R.id.fab).
                    setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            final SelfDialog selfDialog = new SelfDialog(getActivity());
                            selfDialog.setTitle("信息查询");
                            selfDialog.setSchoolArr(schoolList);
                            selfDialog.setMessage("选择以下信息?");
                            selfDialog.setYesOnclickListener("查询", new SelfDialog.onYesOnclickListener() {
                                @Override
                                public void onYesClick(QueryDomain queryDomain) {
                                    mqueryDomain = queryDomain;
                                    page = 1;
                                    HttpClient.queryMember(queryDomain, 1, handler);
                                    selfDialog.dismiss();
                                }

                            });
                            selfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
                                @Override
                                public void onNoClick() {
                                    selfDialog.dismiss();
                                }
                            });
                            selfDialog.show();
                        }
                    });


            swipeRefreshLayout = (RefreshLayout) rootView.findViewById(R.id.swipe_container);
            // 设置下拉圆圈上的颜色，蓝色、绿色、橙色、红色
            swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                    android.R.color.holo_orange_light, android.R.color.holo_red_light);
            swipeRefreshLayout.setDistanceToTriggerSync(400);// 设置手指在屏幕下拉多少距离会触发下拉刷新
            //swipeRefreshLayout.setProgressBackgroundColor(R.color.red); // 设定下拉圆圈的背景
            swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT); // 设置圆圈的大小

            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    page = 1;
                    HttpClient.queryMember(mqueryDomain, 1, handler);
                }
            });

            swipeRefreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
                @Override
                public void onLoad() {
                    swipeRefreshLayout.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // 更新数据 更新完后调用该方法结束刷新
                            swipeRefreshLayout.setLoading(false);
                            HttpClient.queryMember(mqueryDomain, page + 1, handler);
                            page++;
                        }
                    }, 20);

                }

                @Override
                public void setFooterView(boolean isLoading) {
                    if (isLoading) {
                        mlistview.removeFooterView(m_listViewFooter);
                        mlistview.addFooterView(m_listViewFooter);
                    } else {
                        mlistview.removeFooterView(m_listViewFooter);
                    }
                }
            });
            return rootView;
        }
        //教室管理
        if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
            mqueryDomain = new QueryDomain();
            View rootView = inflater.inflate(R.layout.fragment_people, container, false);
            mlistview = (ListView) rootView.findViewById(R.id.listview);
            m_listViewFooter = LayoutInflater.from(getContext())
                    .inflate(R.layout.listview_footer, null, false);
            mlistview.addFooterView(m_listViewFooter);
            mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent(getContext(), ClassInfoActivity.class);
                    intent.putExtra("iD", classAdapter.list.get(i).iD);
                    startActivity(intent);
                }
            });
            page = 1;
            HttpClient.querySchoolClass(mqueryDomain, 1, handler);
            rootView.findViewById(R.id.fab).
                    setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            final SelfDialog selfDialog = new SelfDialog(getActivity());
                            selfDialog.setTitle("信息查询");
                            selfDialog.setSchoolArr(null);
                            selfDialog.setEditTextHint("教室名称");
                            selfDialog.setMessage("选择以下信息?");

                            selfDialog.setYesOnclickListener("查询", new SelfDialog.onYesOnclickListener() {
                                @Override
                                public void onYesClick(QueryDomain queryDomain) {
                                    mqueryDomain = queryDomain;
                                    page = 1;
                                    LogUtils.e(queryDomain.edit.getText().toString());
                                    HttpClient.querySchoolClass(mqueryDomain, 1, handler);
                                    selfDialog.dismiss();
                                }

                            });
                            selfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
                                @Override
                                public void onNoClick() {
                                    selfDialog.dismiss();
                                }
                            });
                            selfDialog.show();
                        }
                    });


            swipeRefreshLayout = (RefreshLayout) rootView.findViewById(R.id.swipe_container);
            // 设置下拉圆圈上的颜色，蓝色、绿色、橙色、红色
            swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                    android.R.color.holo_orange_light, android.R.color.holo_red_light);
            swipeRefreshLayout.setDistanceToTriggerSync(400);// 设置手指在屏幕下拉多少距离会触发下拉刷新
            //swipeRefreshLayout.setProgressBackgroundColor(R.color.red); // 设定下拉圆圈的背景
            swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT); // 设置圆圈的大小

            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    page = 1;
                    HttpClient.querySchoolClass(mqueryDomain, 1, handler);
                }
            });

            swipeRefreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
                @Override
                public void onLoad() {
                    swipeRefreshLayout.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // 更新数据 更新完后调用该方法结束刷新
                            swipeRefreshLayout.setLoading(false);
                            HttpClient.querySchoolClass(mqueryDomain, page + 1, handler);
                            page++;
                        }
                    }, 20);

                }

                @Override
                public void setFooterView(boolean isLoading) {
                    if (isLoading) {
                        mlistview.removeFooterView(m_listViewFooter);
                        mlistview.addFooterView(m_listViewFooter);
                    } else {
                        mlistview.removeFooterView(m_listViewFooter);
                    }
                }
            });
            return rootView;
        }

        //课表管理
        if (getArguments().getInt(ARG_SECTION_NUMBER) == 3) {
            View rootView = inflater.inflate(R.layout.fragment_people, container, false);
            mlistview = (ListView) rootView.findViewById(R.id.listview);
            m_listViewFooter = LayoutInflater.from(getContext())
                    .inflate(R.layout.listview_footer, null, false);
            mlistview.addFooterView(m_listViewFooter);
            mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent(getContext(), TimetableActivity.class);
                    intent.putExtra("iD", timetableAdapter.list.get(i).iD);
                    intent.putExtra("title", timetableAdapter.list.get(i).clName);
                    startActivity(intent);
                }
            });
            page = 1;
            HttpClient.queryTimetable(1, handler);
            rootView.findViewById(R.id.fab).setVisibility(View.GONE);
            swipeRefreshLayout = (RefreshLayout) rootView.findViewById(R.id.swipe_container);
            swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                    android.R.color.holo_orange_light, android.R.color.holo_red_light);
            swipeRefreshLayout.setDistanceToTriggerSync(400);
            swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
            // swipeRefreshLayout.setRefreshing(false);
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    page = 1;
                    HttpClient.queryTimetable(1, handler);
                }
            });

            swipeRefreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
                @Override
                public void onLoad() {
                    swipeRefreshLayout.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // 更新数据 更新完后调用该方法结束刷新
                            swipeRefreshLayout.setLoading(false);
                            HttpClient.queryTimetable(page + 1, handler);
                            page++;
                        }
                    }, 20);

                }

                @Override
                public void setFooterView(boolean isLoading) {
                    if (isLoading) {
                        mlistview.removeFooterView(m_listViewFooter);
                        mlistview.addFooterView(m_listViewFooter);
                    } else {
                        mlistview.removeFooterView(m_listViewFooter);
                    }
                }
            });
            return rootView;
        }
        return null;
    }


    //人员管理listadapter
    public class peopleAdapter extends BaseAdapter {
        private ArrayList<UserInfoBase> list;
        private Context context;

        public peopleAdapter(Context context, ArrayList<UserInfoBase> list) {
            this.context = context;
            this.list = list;
//            LogUtils.e("已创建");
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return Long.parseLong(list.get(i).iD);
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
//            LogUtils.e("已生成");
            View v;
            Viewholder viewholder;
            if (view != null && view instanceof RelativeLayout) {
                v = view;
                viewholder = (Viewholder) v.getTag();
            } else {
                v = View.inflate(context, R.layout.item_peopleinfo, null);
                viewholder = new Viewholder();
                viewholder.userFace = (ImageView) v.findViewById(R.id.item_peopleinfo_iv);
                viewholder.trueName = (TextView) v.findViewById(R.id.item_peopleinfo_name_tv);
                viewholder.stuId = (TextView) v.findViewById(R.id.item_peopleinfo_stuid_tv);
                viewholder.stuClass = (TextView) v.findViewById(R.id.item_peopleinfo_class_tv);
                viewholder.stuPhone = (TextView) v.findViewById(R.id.item_peopleinfo_phone_tv);
                v.setTag(viewholder);
            }
//            viewholder.userFace.setImageBitmap();
            viewholder.trueName.setText(list.get(i).trueName);
            viewholder.stuId.setText(list.get(i).stuId);
            viewholder.stuClass.setText(list.get(i).Class);
            viewholder.stuPhone.setText(list.get(i).phoneNum);
            return v;
        }

        public void addlist(ArrayList<UserInfoBase> list) {
            this.list.addAll(list);
        }
    }

    static class Viewholder {
        ImageView userFace;
        TextView trueName;
        TextView stuId;
        TextView stuClass;
        TextView stuPhone;
    }


    //课表管理listadapter
    public class TimetableAdapter extends BaseAdapter {
        private ArrayList<ClassListDomain> list;
        private Context context;

        public TimetableAdapter(Context context, ArrayList<ClassListDomain> list) {
            this.context = context;
            this.list = list;
//            LogUtils.e("已创建");
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return Long.parseLong(list.get(i).iD);
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v;
            TimetableViewholder viewholder;
            if (view != null && view instanceof RelativeLayout) {
                v = view;
                viewholder = (TimetableViewholder) v.getTag();
            } else {
                v = View.inflate(context, R.layout.item_timtableinfo, null);
                viewholder = new TimetableViewholder();
                viewholder.clName = (TextView) v.findViewById(R.id.item_timetableinfo_sname_tv);
                v.setTag(viewholder);
            }
            viewholder.clName.setText(list.get(i).clName);
            return v;
        }

        public void addlist(ArrayList<ClassListDomain> list) {
            this.list.addAll(list);
        }
    }

    static class TimetableViewholder {
        TextView clName;
    }


    //教室管理listadapter
    public class ClassAdapter extends BaseAdapter {
        private ArrayList<schoolClassBase> list;
        private Context context;

        public ClassAdapter(Context context, ArrayList<schoolClassBase> list) {
            this.context = context;
            this.list = list;
//            LogUtils.e("已创建");
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return Long.parseLong(list.get(i).iD);
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v;
            CViewholder viewholder;
            if (view != null && view instanceof RelativeLayout) {
                v = view;
                viewholder = (CViewholder) v.getTag();
            } else {
                v = View.inflate(context, R.layout.item_classinfo, null);
                viewholder = new CViewholder();
                viewholder.sName = (TextView) v.findViewById(R.id.item_classinfo_sname_tv);
                viewholder.cInfo = (TextView) v.findViewById(R.id.item_classinfo__tv);
                viewholder.minor = (TextView) v.findViewById(R.id.item_classinfo__minorinfo);
                v.setTag(viewholder);
            }
//            viewholder.userFace.setImageBitmap();
            LogUtils.e(list.get(i).sName);
            viewholder.sName.setText(list.get(i).sName);
            viewholder.cInfo.setText(list.get(i).className);
            viewholder.minor.setText(list.get(i).minor);
            return v;
        }

        public void addlist(ArrayList<schoolClassBase> list) {
            this.list.addAll(list);
        }
    }

    static class CViewholder {
        TextView sName;
        TextView cInfo;
        TextView minor;
    }

}
